package com.fg.cloud.factory.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.system.CoderulesPurposeEntity;
import com.fg.cloud.manager.service.system.CodeRulesService;

@Controller
@RequestMapping("/codeRules")
public class CodeRules extends BaseController{
	
	@Autowired
	private CodeRulesService codeRulesService;
	
	/**
	 * Description:生成编码
	 * @param origin
	 * @param facId
	 * @return
	 * @author 陈银龙
	 * @date 2017年9月19日上午11:00:01
	 */
	@RequestMapping("/crate")
	@ResponseBody
	public void createNumber(HttpServletRequest request, HttpServletResponse response, Integer origin){
		try{
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("origin", origin);
			usmap.put("facId", getCurrentFactoryUser().getFacId());
			String result = codeRulesService.findRules(usmap);
			if(result == null){
				doResult(response, false, StatusCode.FINDERROR, "无编码规则", ResultType.FIND_FAULT);
			}
			doResult(response, true, StatusCode.FINDSUCCESS, result, ResultType.FIND_SUCCESS);
		}catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增编码规则
	 * */
	@RequestMapping("/addCodeRules")
	@ResponseBody
	public void addCodeRules(HttpServletRequest request, HttpServletResponse response, String dates){
		try {
			CoderulesPurposeEntity cpe = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(dates), CoderulesPurposeEntity.class);
			if(cpe.getPurposeId() == null || cpe.getRuleList().size() == 0){
				doResult(response, false, StatusCode.LACKVALUE, null,ResultType.INPUT_NULL);
				return;
			}
			cpe.setFacId(getCurrentFactoryUser().getFacId());
			Integer flag = codeRulesService.insertRule(cpe);
			if(flag > 0 ){
				doResult(response, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
			}else{
				doResult(response, false, StatusCode.SAVEERROR, flag,  "新增失败,可能是编码规则已存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.ADD_FAULT);
		}
	}
	
	/**
	 * 删除
	 * */
	@RequestMapping(value = "/deleteRules", method = RequestMethod.POST)
	@ResponseBody
	public void deleteRules(HttpServletRequest request, HttpServletResponse response,String ids){
		try{
			if(null == ids || 0 == ids.length()){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			Long[] id = LongUtils.convert(ids.split("[, ]+"));
			Map<String, Object> usmap = new HashMap<String, Object>();
			FactoryUserVo user = getCurrentFactoryUser();
			usmap.put("ids", id);
			usmap.put("facId", user.getFacId());
			Integer flag = codeRulesService.deleteRules(usmap);
			if(flag > 0 ){
				doResult(response, true, StatusCode.REMOVESUCCESS, flag, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(response, false, StatusCode.REMOVEERROR, flag, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * 查询头
	 * */
	@RequestMapping(value = "/rulesList", method = RequestMethod.POST)
	@ResponseBody
	public void rulesList(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String keyword){
		try {
			if(page == null || page < 0 || rows == null || rows < 0){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			FactoryUserVo user = getCurrentFactoryUser();
			PageUtils<CoderulesPurposeEntity> pageinfo = new PageUtils<CoderulesPurposeEntity>(page, rows, "id", "desc");
			Map<String, Object> usmap = pageinfo.getCondition();
			usmap.put("facId",user.getFacId());
			usmap.put("keyword",keyword);
			codeRulesService.rulesList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	/**
	 * 详情
	 * */
	@RequestMapping(value = "/findDatels", method = RequestMethod.POST)
	@ResponseBody
	public void findDatels(HttpServletRequest request, HttpServletResponse response,Long id){
		try {
			if(id==null || id< 0){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			FactoryUserVo user = getCurrentFactoryUser();
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId",user.getFacId());
			usmap.put("id",id);
			CoderulesPurposeEntity cpe = codeRulesService.findDatels(usmap);
			doResult(response, true, StatusCode.FINDSUCCESS, cpe, ResultType.FIND_SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	/**
	 * 修改编码规则
	 * */
	@RequestMapping("/updateCodeRules")
	@ResponseBody
	public void updateCodeRules(HttpServletRequest request, HttpServletResponse response, String dates){
		try {
			CoderulesPurposeEntity cpe = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(dates), CoderulesPurposeEntity.class);
			if(cpe.getId() == null || cpe.getPurposeId() == null || cpe.getRuleList().size() == 0){
				doResult(response, false, StatusCode.LACKVALUE, null,ResultType.INPUT_NULL);
				return;
			}
			cpe.setFacId(getCurrentFactoryUser().getFacId());
			Integer flag = codeRulesService.updateCodeRules(cpe);
			if(flag > 0 ){
				doResult(response, true, StatusCode.OPERATIONSUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(response, false, StatusCode.OPERATIONERROR, flag,  "保持失败,可能是编码规则已存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.OPERATIONERROR, null, ResultType.SAVE_FAULT);
		}
	}
}
